/**
 * 初始化
 */
var Mail = {
    id: "MailTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Mail.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '标题', field: 'title', align: 'center', valign: 'middle', sortable: true},
        {title: '状态', field: 'read_status', align: 'center', valign: 'middle', sortable: true},
        {title: '接收时间', field: 'created_time', align: 'center', valign: 'middle', sortable: true}
    ];
};

/**
 * 检查是否选中
 */
Mail.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        Mail.seItem = selected[0];
        return true;
    }
};

/**
 * 查看详情
 */
Mail.detail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '查看详情',
            area: ['800px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/hp/mail/detail/' + Mail.seItem.id
        });
        this.layerIndex = index;
    }
};


/**
 * 删除信件
 */
Mail.del = function () {
    if (this.check()) {
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/hp/mail/delete", function (data) {
                Feng.success("删除成功!");
                Mail.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", Mail.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否删除信件?", operation);
    }
}

Mail.formParams = function() {
    var queryData = {};
    return queryData;
}

/**
 * 查询日志列表
 */
Mail.search = function () {
    Mail.table.refresh({query: Mail.formParams()});
};

$(function () {
    var defaultColunms = Mail.initColumn();
    var table = new BSTable(Mail.id, "/hp/mail/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Mail.formParams());
    Mail.table = table.init();
});
